#pragma once

#include "Service/FundsService.h"

class FastBlockQueue;
class MarketStorage;
class Business;
class Market;
class RequestTimeout;
struct OrderNotification;
class DealingRepository;
class Dealing;
class QueueContext;
struct Command;
class IMarketHandler;
class Trade;
struct ExchangeNotify;
struct PendingMarket;
class ContractMatcheres;
class TimeoutNotifier;

class BusinessThread
{
public:
	BusinessThread(MarketStorage& marketStorage, QueueContext& queueContext,
		DealingRepository& dealingRepository,ContractMatcheres& contractMatcheres,
		TimeoutNotifier& timeoutNotifier);
	~BusinessThread();

	void Run();

	void OnOrderNotification(OrderNotification const& orderNotification);
	void OnPendingMarket(PendingMarket const& pendingMarket);
	void OnMarket(Market const& market);
	void OnRequestTimeout(RequestTimeout const& requestTimeout);
	void OnCommand(Command const& command);
	void OnExchangeNotify(int exchangeStatus);

private:
	bool UpdateDealing(OrderNotification const& orderNotification, Dealing& dealing);
	void LogDealing(Dealing& dealing) const;
	void LogOrderNotification(Dealing const& dealing, OrderNotification const& orderNotification) const;
	void AddMarketHandler(IMarketHandler* marketHandler);
	void AddBusiness(Business* business);
	void OnDealing(Dealing const& dealing);
	void OnTrade(Trade const& trade);
	MarketStorage& m_marketStorage;
	QueueContext& m_queueContext;
	DealingRepository& m_dealingRepository;
	ContractMatcheres& m_contractMatcheres;
	TimeoutNotifier& m_timeoutNotifier;
	FundsService m_fundsService;

	typedef std::list<IMarketHandler*> MarketHandlerList;
	MarketHandlerList m_marketHandlers;

	typedef std::vector<Business*> Businesses;
	typedef std::vector<Businesses> ContractBusinessListVector;
	ContractBusinessListVector m_contractBusinessListes;
	Businesses m_businesses;
};